package com.wangbo.box.chain;

public class ChainPattern {

    static abstract class Handler{
        Handler next;

        public void setNext(Handler next) {
            this.next = next;
        }

        void start(){
            invoke();
            if (next != null){
                next.start();
            }
        }
        abstract void invoke();
    }

    static class HandlerA extends Handler{

        @Override
        void invoke() {
            System.out.println("handler A");
        }
    }
    static class HandlerB extends Handler{

        @Override
        void invoke() {
            System.out.println("handler B");
        }
    }

    static class HandlerC extends Handler{

        @Override
        void invoke() {
            System.out.println("handler C");
        }
    }

    public static void main(String[] args) {
        Handler handlerA = new HandlerA();
        Handler head = new HandlerB();
        Handler handlerC = new HandlerC();
        head.setNext(handlerC);
        handlerC.setNext(handlerA);
        head.start();
    }


}
